﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ComLib;
using ComLib.Data;
using ComLib.WebModules.Widgets;


namespace ComLib.Mvc.Lib
{
    public class WidgetHelper
    {
        /// <summary>
        /// Iterate over each widget/instance in the specified zone.
        /// </summary>
        /// <param name="zone"></param>
        /// <param name="executor"></param>
        public static void ForEachInZone(string zone, Action<Widget, WidgetInstance> executor)
        {
            var widgets = Widget.GetRepository().ToLookUp();
            var instances = WidgetInstance.Find(Criteria.New().Where("Zone").Is(zone).OrderBy("SortIndex").Asc);
            foreach (var instance in instances)
            {
                var widget = widgets[instance.WidgetId];
                executor(widget, instance);
            }
        }
    }
}
